package de.gameduell.framework.facebook {
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.facebook.service.gen.GameState;

	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class FBBaseGameState extends EventDispatcher {
		
		public static var ALL_STATES_CHANGED:String = "gamestate_all_state_changed";
		public static var FIRST_TIME_START:String   = "gamestate_first_time_start";
		
		private var _states:Array;
		private var _isFirstTimeUser:Boolean;
		
		public function FBBaseGameState() {
			_states = new Array();
			_isFirstTimeUser = false;
		}
		
		public function get states():Array {
			return _states;
		}
		
		public function storeState(aGameState:GameState):void {
			Debug.trace("FBBaseGameState::storeState: " + aGameState.key + "=" + aGameState.value, Debug.DEBUG);
			_states[aGameState.key] = aGameState;
			dispatchEvent(new FBBaseGameStateEvent(FBBaseGameStateEvent.STATE_CHANGED, aGameState));
		}
		
		public function storeStates(states:Array):void {
			if(states.length > 0){
				for each(var aState:GameState in states){
					storeState(aState);
				}
				dispatchEvent(new Event(ALL_STATES_CHANGED));
			}else{
				_isFirstTimeUser = true;
				dispatchEvent(new Event(FIRST_TIME_START));
			}
		}
		
		public function readState(aKey:String):GameState {
			return GameState(_states[aKey]);
		}
		
		public function readStates():Array {
			var result:Array = new Array();
			for(var i:* in _states){
				result.push(GameState(_states[i]));
			}
			return result;
		}
		
		public function get isFirstTimeUser():Boolean {
			return _isFirstTimeUser;
		}
		
		public function set isFirstTimeUser(isFirst:Boolean):void {
			_isFirstTimeUser = isFirst;
		}
	}
}
